<?php

/***
 * Class CW_ControllerHandlerClass is a parent class of any controller object.
 ***/
class CW_ControllerHandlerClass {

   /**
	* Core registry
	* @access Protected
	* @var Object
	*/
	protected $Reg;


   // ! Constructor Method

   /**
	* Instantiates class and defines instance variables.
	*
	* @param Object $Reg Core registry passed by reference
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access public
	* @return Void
	*/
	public function __construct(&$Reg)
	{
		$this->Reg = &$Reg;
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* XML to the current schema.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function addXml(&$Xml)
	{
		return $this->Reg->Xml->addNode(&$Xml);
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function addView($template, $application = CURRENT_APPLICATION)
	{
		foreach(explode('|', $template) as $template)
		{
			if(trim($template))
			{
				$this->Reg->Xsl->addTemplate(PATH_DESIGN_XSL . "{$template}.view.xsl");
			}
		}

		return true;
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function addVariable($name, $value)
	{
		return $this->Reg->Xsl->addVariable($name, $value);
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function addModel($model, $application = CURRENT_APPLICATION)
	{
		return $this->Reg->Mount->model($model, $application);
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function addFactory($factory, $return_associative = true, $application = CURRENT_APPLICATION)
	{
		if(strpos($factory, '|'))
		{
			$factories = array();

			foreach(explode('|', $factory) as $factory)
			{
				if(trim($factory))
				{
					if(false == $return_associative)
					{
						$factories[] = $this->Reg->Mount->factory($factory, $application);
					}
					else
					{
						$factories[$factory] = $this->Reg->Mount->factory($factory, $application);
					}
				}
			}

			return $factories;
		}

		return $this->Reg->Mount->factory($factory, $application);
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function addLibrary($library, &$parameters = array(), $load_from_core = true, $application = CURRENT_APPLICATION)
	{
		return $this->Reg->Mount->library($library, $parameters, $load_from_core, $application);
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function linkController($application, $controller)
	{
		return $this->Reg->Mount->link($application, $controller);
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function addErrorXml($errors = array())
	{
		return $this->addXml($this->useFactory('Global', 'generateErrorXml', array($errors)));
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function addFormFieldXml($fields, $name = 'form')
	{
		return $this->addXml($this->useFactory('Global', 'generateFormFieldXml', array($name, $fields)));
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function addFormFieldAndErrorXml($errors, $fields, $name = 'form')
	{
		$this->addFormFieldXml($fields, $name);
		$this->addErrorXml($errors);

		return true;
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function getDefinition($definition, $replacements = array())
	{
		if(false == $this->getConfig('enable', 'locale', CURRENT_APPLICATION) || false == $this->Reg->checkIfObjectExists('Locale'))
		{
			throw new CoreException("Definition '{$definition}' could not be located because locale functionality is not enabled!");
		}

		if(false == $output = $this->Reg->Locale->getLocaleDefinition($definition, $replacements))
		{
			throw new CoreException("Definition '{$definition}' does not exist!");
		}

		return $output;
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function getConfig($item, $group = 'system', $application = CURRENT_APPLICATION)
	{
		return  $this->Reg->Config->get($item, $group, $application);
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function addExtension($extension)
	{
		return $this->Reg->Mount->extension($extension);
	}


   // ! Executor Method

   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function getInput($name, $stack = 'get')
	{
		return $this->Reg->Input->get($name, $stack);
	}


   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function setInput($name, $value, $stack = 'get')
	{
		return $this->Reg->Input->set($name, $value, $stack);
	}


   /**
	* Convenience method that allows a user to easily add
	* views to the current XSL document.
	*
	* @param Object $Xml The xml to add
	* @author Daniel Wilhelm II Murdoch <wilhelm.murdoch@gmail.com>
	* @since v 1.0.0
	* @access Protected
	* @return True
	*/
	protected function getUrlSegment($segment)
	{
		return $this->Reg->Url->getUrlSegment($segment);
	}
}

?>